package com.example.qt.ptplatform.service.impl;

import com.example.qt.ptplatform.entity.Comments;
import com.example.qt.ptplatform.mapper.CommentMapper;
import com.example.qt.ptplatform.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void saveComment(Integer torrentId, Integer userId, String content, Boolean isAnonymous) {
        Comments comment = new Comments();
        comment.setTorrentId(torrentId);
        comment.setUserId(userId);
        comment.setContent(content);
        comment.setIsAnonymous(isAnonymous);
        comment.setCommentTime(new Date());
        commentMapper.insert(comment);
    }

    @Override
    public void saveReply(Integer torrentId, Integer userId, String content, Boolean isAnonymous, Integer comCommentId) {
        Comments reply = new Comments();
        reply.setTorrentId(torrentId);
        reply.setUserId(userId);
        reply.setContent(content);
        reply.setIsAnonymous(isAnonymous);
        reply.setComCommentId(comCommentId);  // 设置为回复的原评论的 ID
        reply.setCommentTime(new Date());
        commentMapper.insert(reply);

        // 更新原评论的 replyCount
        Comments originalComment = commentMapper.selectById(comCommentId);
        if (originalComment != null) {
            originalComment.setReplyCount(originalComment.getReplyCount() + 1);
            commentMapper.updateById(originalComment);
        }
    }

    @Override
    public void likeComment(Integer commentId) {
        // 获取评论对象
        Comments comment = commentMapper.selectById(commentId);
        if (comment != null) {
            // 点赞数加1
            comment.setLikesCount(comment.getLikesCount() + 1);
            commentMapper.updateById(comment);
        }
    }

    @Override
    public void unlikeComment(Integer commentId) {
        // 获取评论对象
        Comments comment = commentMapper.selectById(commentId);
        if (comment != null && comment.getLikesCount() > 0) {
            // 点赞数减1
            comment.setLikesCount(comment.getLikesCount() - 1);
            commentMapper.updateById(comment);
        }
    }

    @Override
    public void deleteComment(Integer commentId) {
        // 删除评论
        Comments comment = commentMapper.selectById(commentId);
        if (comment != null) {
            // 如果评论有回复，更新回复的评论数
            if (comment.getComCommentId() != null) {
                Comments parentComment = commentMapper.selectById(comment.getComCommentId());
                if (parentComment != null) {
                    parentComment.setReplyCount(parentComment.getReplyCount() - 1);
                    commentMapper.updateById(parentComment);
                }
            }

            // 删除评论
            commentMapper.deleteById(commentId);
        }
    }

    @Override
    public List<Comments> getAllCommentsWithReplies() {
        // 获取所有评论（comCommentId为null的评论）
        List<Comments> allComments = commentMapper.selectList(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Comments>().isNull("com_comment_id")
        );

        // 获取每个评论的回复
        for (Comments comment : allComments) {
            List<Comments> replies = commentMapper.selectList(
                    new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Comments>().eq("com_comment_id", comment.getCommentId())
            );
            comment.setReplyCount(replies.size());  // 更新回复数
        }

        return allComments;
    }
}
